//
//  TempateCareTwo.m
//  LeaveLight
//
//  Created by pro1 on 2019/3/11.
//  Copyright © 2019 xsl. All rights reserved.
//

#import "TempateCareTwo.h"
#import "TheColorLabel.h"

@interface TempateCareTwo ()

@property (nonatomic, strong) UIButton * shareBtn;
@property (nonatomic, strong) UIButton * collectBtn;
@property (nonatomic, strong) UIImageView * baseImgView;
@property (nonatomic, strong) QMUIZoomImageView * zoomimage;
//@property (nonatomic, strong) QMUITextView *describleTextView;
@property (strong, nonatomic) TheColorLabel *timeLabel;

@end

@implementation TempateCareTwo

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setShadow:CGSizeMake(0,2) corner:10 opacity:1 color:[UIColor whiteColor] shadowColor:RGB(153, 153, 153) shadowRadius:6];
        [self addSubview:self.baseImgView];
        [self addSubview:self.zoomimage];
        [self addSubview:self.describleTextView];
        [self addSubview:self.authorTextField];
        [self addSubview:self.shareBtn];
        [self addSubview:self.collectBtn];
    }
    return self;
}

#pragma mark - private
- (UIButton *)shareBtn
{
    if (!_shareBtn) {
        _shareBtn = [[UIButton alloc]init];
        [_shareBtn setImage:[UIImage imageNamed:@"btn_home_share"] forState:0];
        [_shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shareBtn;
}

- (UIButton *)collectBtn
{
    if (!_collectBtn) {
        _collectBtn = [[UIButton alloc]init];
        [_collectBtn setImage:[UIImage imageNamed:@"btn_home_collect"] forState:UIControlStateNormal];
        [_collectBtn setImage:[UIImage imageNamed:@"btn_home_collect_sel"] forState:UIControlStateSelected];
        [_collectBtn addTarget:self action:@selector(collectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _collectBtn;
}

- (UIImageView *)baseImgView
{
    if (!_baseImgView) {
        _baseImgView = [[UIImageView alloc]init];
        [_baseImgView setContentMode:UIViewContentModeScaleAspectFill];
    }
    return _baseImgView;
}

- (QMUIZoomImageView *)zoomimage
{
    if (!_zoomimage) {
        _zoomimage = [[QMUIZoomImageView alloc]init];
        [_zoomimage setContentMode:UIViewContentModeScaleAspectFill];
        _zoomimage.viewportRect = CGRectZero;
        _zoomimage.userInteractionEnabled = NO;
    }
    return _zoomimage;
}

- (QMUITextView *)describleTextView
{
    if (!_describleTextView) {
        _describleTextView = [[QMUITextView alloc]init];
        _describleTextView.userInteractionEnabled = NO;
        _describleTextView.backgroundColor = [UIColor clearColor];
        NSMutableParagraphStyle * style = [NSMutableParagraphStyle new];
        style.lineSpacing = 10;
        _describleTextView.typingAttributes = @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 10],NSKernAttributeName: @(-1),NSParagraphStyleAttributeName: style};
        _describleTextView.maximumTextLength = 50;
//        _describleTextView.shouldCountingNonASCIICharacterAsTwo = YES;
    }
    return _describleTextView;
}

- (LogoTipView *)authorTextField
{
    if (!_authorTextField) {
        _authorTextField = [[LogoTipView alloc]init];
        _authorTextField.style = LogoTipViewStyleText;
    }
    return _authorTextField;
}

- (TheColorLabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[TheColorLabel alloc] initWithFrame:CGRectMake(20, CGRectGetHeight(self.baseImgView.frame) - 15, SCREEN_WIDTH - 40, 30)];
        _timeLabel.text = [SystemManager getDateWith:@","];
    }
    return _timeLabel;
}



#pragma mark - action
- (void)shareBtnClick:(UIButton *)btn
{
    weakify(self);
    self.shareBtn.hidden = YES;
    self.collectBtn.hidden = YES;
    UIImage * shareimg = [self qmui_snapshotImageAfterScreenUpdates:YES];
    self.shareBtn.hidden = NO;
    self.collectBtn.hidden = NO;
    [ShareView initWithShareChooseViewChooseBlock:^(NSInteger selIndex) {
        strongify(self);
        if (selIndex < 999 && [[UMSocialManager defaultManager] isInstall:selIndex]) {
            UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
            UMShareImageObject * shareObject = [UMShareImageObject shareObjectWithTitle:@"留光" descr:@"一抹时光 一抹情" thumImage:shareimg];
            shareObject.shareImage = shareimg;
            //分享消息对象设置分享内容对象
            messageObject.shareObject = shareObject;
            [[UMSocialManager defaultManager] shareToPlatform:selIndex messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
                if (error) {
                    [QMUITips showError:@"分享失败"];
                }else{
                    [QMUITips showSucceed:@"分享成功"];
                }
            }];
        }else if(selIndex == 999){
            //保存本地
            UIImageWriteToSavedPhotosAlbum(shareimg, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
        }else{
            [QMUITips showWithText:@"分享失败"];
        }
    }];
    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error == nil) {
        [QMUITips showSucceed:@"保存成功"];
    }else{
        [QMUITips showError:@"保存失败"];
    }
}

- (void)collectBtnClick:(UIButton *)btn
{
    if (btn.selected) {
        [self deleteCollectWithid:self.baseModel.ID Success:^{
            btn.selected = NO;
        } failed:^(NSString * _Nonnull error) {
            [QMUITips showWithText:error];
        }];
    }else
    {
        [self addCollectWithid:self.baseModel.ID imgUrl:self.baseModel.images template_type:self.baseModel.template_type Success:^{
            btn.selected = YES;
        } failed:^(NSString * _Nonnull error) {
            [QMUITips showWithText:error];
        }];
    }
}


#pragma mark - model赋值
- (void)setCardModel:(TemplateModel *)baseModel
{
    self.baseModel = baseModel.chicken_model;
    self.authorTextField.style = baseModel.thescaleStyle ? baseModel.thescaleStyle : LogoTipViewStyleText;
    
    [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(self);
        make.height.offset(40);
        make.width.offset(40);
    }];
    
    [self.collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.right.equalTo(self.shareBtn.mas_left).offset(0);
        make.height.offset(40);
        make.width.offset(40);
    }];
    
    [self.authorTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
        make.bottom.equalTo(self.mas_bottom).offset(-20);
        make.height.offset(30);
    }];
    
    if (self.baseModel.template_type == 4) {
        
        [self.describleTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(14);
            make.right.equalTo(self).offset(-14);
            make.bottom.equalTo(self.authorTextField.mas_top).offset(-24);
            make.height.offset(85);
        }];
        
        [self.baseImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self).offset(0);
            make.bottom.equalTo(self.describleTextView.mas_top).offset(-17);
        }];
        
        [self.zoomimage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(36);
            make.left.equalTo(self).offset(35);
            make.right.equalTo(self).offset(-34);
            make.bottom.equalTo(self.describleTextView.mas_top).offset(-24);
        }];
        
    }else if (self.baseModel.template_type == 5){
        
        [self.baseImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.equalTo(self).offset(0);
        }];
        [self.zoomimage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(56);
            make.right.equalTo(self).offset(-50);
            make.top.equalTo(self).offset(57);
//            make.height.offset(201);
            make.width.equalTo(self.zoomimage.mas_height).multipliedBy(1.0f);
        }];
        
        [self.describleTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(43);
            make.right.equalTo(self).offset(0);
            make.top.equalTo(self.zoomimage.mas_bottom).offset(80);
            make.height.offset(85);
        }];
    }
    [self layoutIfNeeded];
    
    if (self.baseModel.template_type == 5) {
        [self.zoomimage setCorner:self.zoomimage.qmui_width/2];
    }
    
    self.collectBtn.selected = self.baseModel.is_collect == 1 ? NO : YES;
    if ([[self getViewcontrollerView:self] isKindOfClass:[EditController class]]) {
        self.shareBtn.hidden = YES;
        self.collectBtn.hidden = YES;
    }else{
        self.shareBtn.hidden = NO;
        self.collectBtn.hidden = NO;
    }
    
    self.zoomimage.userInteractionEnabled = self.baseModel.is_edit;
    self.authorTextField.userInteractionEnabled = self.baseModel.is_edit;
    self.describleTextView.userInteractionEnabled = self.baseModel.is_edit;
    
    _baseImgView.clipsToBounds = YES;
    if (self.baseModel.template_type == 4)
    {
        [self addSubview:self.timeLabel];
        [self.baseImgView setTopCorner:10];
        self.describleTextView.textColor = UIColorMakeWithHex(@"#202020");
    }else if(self.baseModel.template_type == 5){
        _describleTextView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.3];
        [self.baseImgView setCorner:10];
        self.describleTextView.textColor = UIColorMakeWithHex(@"#FFFFFF");
    }
    self.describleTextView.text = self.baseModel.content;
    self.describleTextView.scrollsToTop = YES;
    [self.describleTextView setContentOffset:CGPointMake(0, 0)];
    //设置毛玻璃
    if (!UIAccessibilityIsReduceTransparencyEnabled()) {
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
        blurEffectView.frame = self.baseImgView.bounds;
        [self.baseImgView addSubview:blurEffectView];
    }
    
    if (baseModel.choiseImage) {
        self.zoomimage.image = baseModel.choiseImage;
        self.baseImgView.image = baseModel.choiseImage;
    }
    else{
        [self.baseImgView sd_setImageWithURL:[NSURL URLWithString:self.baseModel.images]];
        [self.zoomimage.imageView sd_setImageWithURL:[NSURL URLWithString:self.baseModel.images] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            self.zoomimage.image = image;
        }];
    }
    self.backgroundColor = baseModel.bgColor ? baseModel.bgColor : [UIColor whiteColor];
    self.describleTextView.font = baseModel.font ? [baseModel.font fontWithSize:14] : [UIFont systemFontOfSize:14];
    self.authorTextField.font = baseModel.font ? [baseModel.font fontWithSize:12] : [UIFont systemFontOfSize:12];
    
}

@end
